package cn.water.luban.demo.gateway.filter;

import cn.water.luban.demo.gateway.ex.GatewayException;
import com.alibaba.nacos.common.utils.CollectionUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Component
public class AuthenticationFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        try{
            //取数据
            String token = gainToken(exchange);
            return chain.filter(exchange);
        }catch (GatewayException e){
//            String message = e.getMessage();
//            ServerHttpResponse response = exchange.getResponse();
//            byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
//            DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
//            DataBuffer wrap = dataBufferFactory.wrap(bytes);
//            return response.writeWith(Mono.fromSupplier(() -> wrap));
            return Mono.error(e);
        }
    }

    private String gainToken(ServerWebExchange exchange) throws RuntimeException{
        ServerHttpRequest request = exchange.getRequest();
        MultiValueMap<String ,String> queryParams = request.getQueryParams();
        String token = null;
        if(CollectionUtils.isNotEmpty(queryParams.get("token"))){
            token = queryParams.get("token").get(0);
        }
        if(token == null){
            HttpHeaders headers = request.getHeaders();
            List<String> authentication = headers.get("Authorization");
            if (CollectionUtils.isNotEmpty(authentication)){
                token = authentication.get(0);
            }
        }
        if(token == null){
            MultiValueMap<String, HttpCookie> cookies = request.getCookies();
            List<HttpCookie> cookieToken = cookies.get("token");
            if(CollectionUtils.isNotEmpty(cookieToken)){
                token = cookieToken.get(0).getValue();
            }

        }
        if(token == null){
            throw new GatewayException ("token没有携带票据");
        }
        boolean matches = token.matches("\\d{10}");
        if(!matches){
            throw new GatewayException ("你的token格式不正确");
        }
        return token;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
